// efform_onload = function ()
//{
//	$('#result_add-0-fdMiningName').val($('#fdMiningName').val());
//};
/*根据主表查询明细*/
efgrid_onRowClicked = function( grid_id, row_index ){ 
	if(grid_id == "ef_grid_result"){ 
		var grid = efgrid.getGridObject(grid_id); 
		var selectedRow=grid.getRowData(row_index);
		for(var item in selectedRow){
			var colname=item;
			var colvalue=selectedRow[item];
			$('#ef_region_mainadd').find('#result_add-0-'+colname).val(colvalue);
		} 
		var dateTime=$('#result_add-0-fdReportSx').val();
		var date1=dateTime.substring(0,10); 
		var date2=dateTime.substring(11); 
		$('#result_add-0-fdReportSxStartDate').val(date1);
		$('#result_add-0-fdReportSxEndDate').val(date2);
	}
}


var ajax_callback = {
		onSuccess : function(eiInfo) {
			var grid = efgrid.getGridObject("ef_grid_result");
			grid.refresh(eiInfo);
		}, 
		onFail:function(eMsg)
		{
			alert("确认失败，原因："+eMsg);
		}

}	

button_insert_onclick = function (){
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }
  	
	var date1=$('#result_add-0-fdReportSxStartDate').val();
	var date2=$('#result_add-0-fdReportSxEndDate').val();
	$('#result_add-0-fdReportSx').val(date1+"~"+date2);  
	
	var ei_info=new EiInfo();
	ei_info.setByNodeObject(document.forms[0]); 
	if(ei_info.getBlock('result_add').getRows(0)[0][10]==""){
		EFAlert("主要局部措施不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][11]==""){
		EFAlert("临界指标不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][12]==""){
		EFAlert("危险因素或安全隐患不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][13]==""){
		EFAlert("下一步措施不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][2]==""){
		EFAlert("监控时限不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][5]==""){
		EFAlert("矿责任人不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][6]==""){
		EFAlert("公司监控人不能为空");
		return;
	}
	
	var info = getDetail("ef_region_mainadd","result_add");
	if (null != info) {
		EiCommunicator.send("ZMCH30", "insert", info, ajax_callback);
	}
	window.location.reload(); 
}

button_update_onclick = function (){
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }
  	
	var date1=$('#result_add-0-fdReportSxStartDate').val();
	var date2=$('#result_add-0-fdReportSxEndDate').val();
	$('#result_add-0-fdReportSx').val(date1+"~"+date2);
	
	var status=$('#result_add-0-fdStatus').val();
	
	var ei_info=new EiInfo();
	ei_info.setByNodeObject(document.forms[0]); 
	if(ei_info.getBlock('result_add').getRows(0)[0][10]==""){
		EFAlert("主要局部措施不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][11]==""){
		EFAlert("临界指标不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][12]==""){
		EFAlert("危险因素或安全隐患不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][13]==""){
		EFAlert("下一步措施不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][2]==""){
		EFAlert("监控时限不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][5]==""){
		EFAlert("矿责任人不能为空");
		return;
	}
	if(ei_info.getBlock('result_add').getRows(0)[0][6]==""){
		EFAlert("公司监控人不能为空");
		return;
	}
	
	var info = getDetail("ef_region_mainadd","result_add");
	if (null != info && status != "上报") {
		EiCommunicator.send("ZMCH30", "update", info, ajax_callback);
	}else{
		EFAlert("只能对草拟的记录进行修改！"); return;
	} 
	window.location.reload(); 
}


button_query_onclick = function (){
	efgrid.submitInqu( "ef_grid_result", "ZM","ZMCH30","query");
}

button_delete_onclick = function (){
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_result", "ZM","ZMCH30","delete",true);
	}); 
}


button_report_onclick = function (){
	if (isSelected("ef_grid_result") == 0)
		return; 
	efgrid.submitForm( "ef_grid_result", "ZM","ZMCH30","report",true);
}


function change2Date2String()
{

	
}
function check() {
	
	var fdReportCs = $("#result_add-0-fdReportCs").val().trim(); // 主要局部技术措施
	var fdReportLjzb = $("#result_add-0-fdReportLjzb").val().trim(); // 临界指标
	var fdReportYh = $("#result_add-0-fdReportYh").val().trim(); // 危险因素或安全隐患
	var fdReportNext = $("#result_add-0-fdReportNext").val().trim(); // 下一步措施
	var fdReportZrr = $("#result_add-0-fdReportZrr").val().trim(); // 矿责任人
	var fdReportGszrr = $("#result_add-0-fdReportGszrr").val().trim(); // 公司监控人
	var fdRemark = $("#result_add-0-fdRemark").val().trim(); // 备注
	var fdReportSxStartDate = $("#result_add-0-fdReportSxStartDate").val();
	var fdReportSxEndDate = $("#result_add-0-fdReportSxEndDate").val();

	if (fdReportCs == "") {
		SCZXAlert("主要局部技术措施不能为空！");
		return false;
	} else if (fdReportCs.getBytes() > 400
			|| fdReportCs.getBytes() < 1) {
		SCZXAlert("[主要局部技术措施]请输入1~400个字符！");
		return false;
	}
	if (fdReportLjzb == "") {
		SCZXAlert("临界指标不能为空！");
		return false;
	} else if (fdReportLjzb.getBytes() > 100
			|| fdReportLjzb.getBytes() < 1) {
		SCZXAlert("[临界指标]请输入1~100个字符！");
		return false;
	}
	if (fdReportYh == "") {
		SCZXAlert("危险因素或安全隐患不能为空！");
		return false;
	} else if (fdReportYh.getBytes() > 400
			|| fdReportYh.getBytes() < 1) {
		SCZXAlert("[危险因素或安全隐患]请输入1~400个字符！");
		return false;
	}
	
	if (fdReportNext == "") {
		SCZXAlert("下一步措施不能为空！");
		return false;
	} else if (fdReportNext.getBytes() > 500
			|| fdReportNext.getBytes() < 1) {
		SCZXAlert("[下一步措施]请输入1~500个字符！");
		return false;
	}
		
	if (fdReportZrr == "") {
		SCZXAlert("矿责任人不能为空！");
		return false;
	} else if (fdReportZrr.getBytes() > 10
			|| fdReportZrr.getBytes() < 1) {
		SCZXAlert("[矿责任人]请输入1~10个字符！");
		return false;
	}

	if (fdReportGszrr == "") {
		SCZXAlert("公司监控人不能为空！");
		return false;
	} else if (fdReportGszrr.getBytes() > 20
			|| fdReportGszrr.getBytes() < 1) {
		SCZXAlert("[公司监控人]请输入1~20个字符！");
		return false;
	}
	
	if (fdRemark.getBytes() > 250) {
		SCZXAlert("[备注]不能超过250个字符！");
		return false;
	}

	if (fdReportSxStartDate == "" || fdReportSxStartDate == 0
			|| fdReportSxEndDate == "" || fdReportSxEndDate == 0) {
		SCZXAlert("监控时限不能为空！");
		return false;
	} else {
		if (getDateValue(fdReportSxStartDate + " 00:00:00") > getDateValue(fdReportSxEndDate + " 00:00:00")) {
			SCZXAlert("监控开始时间不能晚于监控结束时间，请重新输入！");
			return false;
		}
	}
	
	return true;
}

String.prototype.getBytes = function() {      
    var cArr = this.match(/[^\x00-\xff]/ig);      
    return this.length + (cArr == null ? 0 : cArr.length);   
      
}  

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
 }

/*
根据传入的String类型的date获取该日期对应的毫秒值，传入的字符串格式为：yyyy-MM-dd HH:mm:ss
*/
function getDateValue(dateStr) {
   var tt = dateStr.split(" ");
   var ttt1 = tt[0].split("-");
   var ttt2 = tt[1].split(":");
   var date1 = new Date(ttt1[0], ttt1[1], ttt1[2], ttt2[0], ttt2[1], ttt2[2]);
   var m1 = date1.getTime();
   return m1;
}

